<template>
    <div class="box">
        <header class="header">
            <van-nav-bar
                title="新增收货地址"
                left-text="返回"
                left-arrow
                @click-left="$router.back()"
            />
        </header>
        <div class="content">
            <van-address-edit
                :area-list="areaList"
                show-postal
                show-delete
                show-set-default
                show-search-result
                :search-result="searchResult"
                :area-columns-placeholder="['请选择', '请选择', '请选择']"
                @save="onSave"
                @delete="onDelete"
                @change-detail="onChangeDetail"
                @change-default="asDefaultAddress"
            />
        </div>
    </div>
</template>

<script>
import Vue from 'vue'
import { Area, Toast, AddressEdit } from 'vant'
import { areaList } from '@vant/area-data'
import { update, updateOrderAddress, addressAdd } from '../../api/order'

Vue.use(Area)
Vue.use(Toast)
Vue.use(AddressEdit)
export default {
  data () {
    return {
      areaList,
      searchResult: [],
      value:''
    }
  },
  mounted () {
    // console.log(this.$route.params.time);
  },
  methods: {
    asDefaultAddress (value) {
      this.value = value
      // console.log(this.value);
    },
    // 新增收货地址的保存
    onSave (content) {
      console.log(content);
      content.userid = localStorage.getItem('userid')  
      content.isDefault = this.value  // 获取设置默认地址按钮的状态
      Toast('save')
      //  添加订单的地址 ******************
      addressAdd(
          // userid: localStorage.getItem('userid'),
          // name: content.name,
          // tel: content.tel,
          // province: content.province,
          // city: content.city,
          // county: content.county,
          // addressDetail: content.addressDetail,
          // isDefault: true  //默认地址
          content
      ).then(res => {
          console.log(res)
          content.time = this.$route.params.time
          //  修改订单的地址 ******************
          updateOrderAddress(
            // userid: localStorage.getItem('userid'),
            // name: content.name,
            // tel: content.tel,
            // province: content.province,
            // city: content.city,
            // county: content.county,
            // addressDetail: content.addressDetail,
            // time: this.$route.params.time
            content
          ).then(res => {
              // console.log(res)
          })
      })
      this.$router.push('/order')
    },
    onDelete () {
      Toast('delete')
    },
    onChangeDetail (val) {
      if (val) {
        this.searchResult = [
          {
            name: '黄龙万科中心',
            address: '杭州市西湖区'
          }
        ]
      } else {
        this.searchResult = []
      }
    }
  }
}
</script>
